//
//  TXTabBar.swift
//  Here_Swift
//
//  Created by 童煊 on 2017/12/25.
//  Copyright © 2017年 童煊. All rights reserved.
//

import UIKit

protocol TXTabBarDelegate:NSObjectProtocol{
    
    func tabBarPlusBtnClick(tabBar:TXTabBar) -> Void;
    
}


class TXTabBar: UITabBar {
    
    let TXTabBarMargin:CGFloat = 10

    /** 代理*/
    public weak var tabBarDelegate:TXTabBarDelegate?
    /** 按钮*/
    var plusBtn = UIButton.init(type: UIButtonType.custom)
    
    override var frame: CGRect{
        didSet{
            var newFrame = frame
            if self.superview != nil && self.superview?.bounds.maxY != newFrame.maxY {
                newFrame.origin.y = (self.superview?.bounds.height)! - newFrame.height
                
            }
            super.frame = newFrame
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.shadowImage = UIImage.imageWithColor(color: UIColor.clear)
        self.plusBtn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_icon_public1"), for: UIControlState.normal)
        self.plusBtn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_icon_public1"), for: UIControlState.highlighted)
        self.addSubview(self.plusBtn)
        self.plusBtn.frame.size = CGSize.init(width: (self.plusBtn.currentBackgroundImage?.size.width)!, height: (self.plusBtn.currentBackgroundImage?.size.height)!)
        self.plusBtn.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self.frame.size.height*0.5 - (isiPhoneX ? (self.TXTabBarMargin+17) : self.TXTabBarMargin))
        }
        self.plusBtn.reactive.controlEvents(UIControlEvents.touchUpInside).observe { (touch) in
            print("点击plus button")
            if self.tabBarDelegate != nil{
                self.tabBarDelegate?.tabBarPlusBtnClick(tabBar: self)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let tabBarBtnClass = NSClassFromString("UITabBarButton")
        
        self.plusBtn.snp.remakeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self.frame.size.height*0.5 - (isiPhoneX ? (self.TXTabBarMargin+17) : self.TXTabBarMargin))
            make.height.equalTo((self.plusBtn.currentBackgroundImage?.size.height)!)
            make.width.equalTo((self.plusBtn.currentBackgroundImage?.size.width)!)
        }

        var btnIndex = 0
        for btn:UIView in self.subviews {
            if btn.isKind(of: tabBarBtnClass!){
                btn.frame.size.width = self.frame.size.width/3
                btn.frame.origin.x = btn.frame.size.width * CGFloat(btnIndex)
                btnIndex+=1
                if btnIndex == 1{
                    btnIndex+=1
                }
            }
        }
        self.bringSubview(toFront: self.plusBtn)
    }
    
    
}
